package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcMenuService;
import com.soft.idc.common.service.IdcOrgService;
import com.soft.idc.common.util.JQPage;

public class OrgManagerController extends Controller {
	public void index() {
		this.render("/menu/menu.html");
	}

	public void queryOrg() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String org_name = this.getPara("org_name");
		String parent_org_name = this.getPara("parent_org_name");
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT O.ORG_ID, O.PARENT_ID, ");
		sb.append("O.ADDRESS, O.EMAIL, O.LOGO, ");
		sb.append("O.MEMO, O.NAME, O.ORG_TYPE, ");
		sb.append("(SELECT NAME FROM IDC_ORG OP ");
		sb.append("WHERE OP.ORG_ID = O.PARENT_ID) AS PARENT_NAME ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("FROM IDC_ORG O LEFT JOIN IDC_ORG PO ON PO.ORG_ID = O.PARENT_ID WHERE 1=1 ");
		if(org_name !=null ){
			sqlFrom.append(" AND O.NAME LIKE '%" + org_name + "%' ");
		}
		
		if(parent_org_name !=null ){
			sqlFrom.append(" and PO.NAME like '%" + parent_org_name + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		this.renderJson(jPage);
	}

	public void add() {
		String parent_id = this.getPara("parent_id");
		String org_name = this.getPara("org_name");
		String address = this.getPara("address");
		String email = this.getPara("email");
		String memo = this.getPara("memo");
		String logo = this.getPara("logo");
		String org_type = this.getPara("org_type");
		String sql = "INSERT INTO IDC_ORG (PARENT_ID, NAME, ADDRESS, EMAIL, MEMO, LOGO, ORG_TYPE ) VALUES (? , ? , ?, ?, ?, ?,?)";
		int  result = Db.update(sql, new Object[]{parent_id, org_name, address, email, memo, logo, org_type});
		this.renderJson(result);
	}

	public void delete() {
		final String orgIds = this.getPara("orgIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] roleIdArr = orgIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_ORG WHERE ORG_ID IN(");
				for(int i=0; i<roleIdArr.length; i++){
					if(i==roleIdArr.length-1){
						sb.append(roleIdArr[i] + ")");
					}else{
						sb.append(roleIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==roleIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String org_id = this.getPara("org_id");
		String parent_id = this.getPara("parent_id");
		String name = this.getPara("name");
		String address = this.getPara("address");
		String email = this.getPara("email");
		String memo = this.getPara("memo");
		String logo = this.getPara("logo");
		String org_type = this.getPara("org_type");
		String sql = "UPDATE IDC_ORG SET PARENT_ID=?, NAME=?, ADDRESS=?, EMAIL=?, MEMO=?, LOGO=? , ORG_TYPE=? WHERE ORG_ID =? ";
		int  result = Db.update(sql, new Object[]{parent_id, name, address, email, memo, logo, org_type, org_id});
		this.renderJson(result);
	}
	
	public void queryOrgList(){
		List<Record> result= IdcOrgService.loadOrg();
		this.renderJson(result);
	}
	
	public void queryParentList(){
		List<Record> result= IdcOrgService.loadParentOrg();
		this.renderJson(result);
	}
	
	public void querySubOrgList(){
		IdcUser u = this.getSessionAttr("SYS_USER");
		List<Record> result= IdcOrgService.querySubOrg(u.getOrgId());
		this.renderJson(result);
	}
}
